package com.yzy.soft.system.service.impl;

import com.yzy.soft.system.entity.ZipStreamEntity;
import com.yzy.soft.system.service.FileService;
import com.yzy.soft.system.util.ZipUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class FileServiceImpl implements FileService {
    private final static Logger logger= LoggerFactory.getLogger(CodeGenerateServiceImpl.class);


    @Autowired
    ZipUtils zipUtils;
    @Override
    public File zipTools(List<File> fileList,String targetFileName) throws FileNotFoundException,IOException {
        logger.info("FileServiceImpl zipTools");

        List<ZipStreamEntity> list = new ArrayList<>();
        InputStream fileInputStream = null;
        targetFileName += ".zip";

        File targetFile=new File(targetFileName);
        targetFile.deleteOnExit();

            targetFile.createNewFile();


        for (int i = 0; i <fileList.size() ; i++) {
            File file = fileList.get(i);

            logger.info("待打包文件的文件路径： {}",file.getPath());
            logger.info("待打包文件的文件名称： {}",file.getName());

             fileInputStream = new FileInputStream(file);
            list.add(new ZipStreamEntity(file.getName(), fileInputStream));
        }
        return ZipUtils.listStreamToZipFile(list, targetFile);
    }
}
